package com.sheep.opt;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2011-7-26
 * Time: 11:24:58
 * To change this template use File | Settings | File Templates.
 */
public abstract class AbstractForm {
    private boolean isValid = false;
    private List<String> validMsg = null;
    
    public AbstractForm(){
        getParameter();
        isValid = valid();
    }

    public int getInt(HttpServletRequest request, String param, int def){
        try{
            return Integer.parseInt(request.getParameter(param).toString().trim());
        }catch(Exception e){
            return def;
        }
    }

    public String getString(HttpServletRequest request, String param, String def){
        try{
            return request.getParameter(param).toString().trim();
        }catch(Exception e){
            return def;
        }
    }

    public boolean getBoolean(HttpServletRequest request, String param, boolean def){
        try{
            return Boolean.parseBoolean(request.getParameter(param).toString().trim());
        }catch(Exception e){
            return def;
        }
    }

    public boolean valid(){
        if(validMsg == null){
            validMsg = new ArrayList<String>();
            isValid = initValid();
        }
        return isValid;
    }
    
    protected abstract void getParameter();
    protected abstract boolean initValid();
}
